/* @file SHMenuBar.Java MenuBar implementation
 * @author Jay Schulist jjschlst@gmail.com
 * 
 * Copyright (C) 2007 Jay Schulist
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package stickyhand.ui;

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class SHMenuBar extends JMenuBar {
	private StickyHand app;
	private boolean onMacOSX = false;
	private int numWindows = 0;
	
	public SHMenuBar(StickyHand sh) {
		super();
		app = sh;
		if (System.getProperty("mrj.version") != null)
			onMacOSX = true;
		createMenu();
	}
	
	/** File Window Help */
	public static int getMenuIndex(String name) {
		if (name.equals("File"))
			return 0;
		else if (name.equals("Window"))
			return 1;
		else if (name.equals("Help"))
			return 2;
		else
			return -1;
	}
	
	protected void addWindow(Component c) {
		JMenu winM = this.getMenu(SHMenuBar.getMenuIndex("Window"));
		if (numWindows == 0)
			winM.addSeparator();
		winM.add(c);
		numWindows++;
	}
	
	protected void removeWindow(Component c) {
		JMenu winM = this.getMenu(SHMenuBar.getMenuIndex("Window"));
		winM.remove(c);
		numWindows--;
		if (numWindows == 0)
			winM.remove(winM.getComponentCount());
	}
	
	private JMenu createFileMenu() {
		JMenu menu = new JMenu("File");
		JMenuItem item;
	
		item = new JMenuItem("Open File...");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		item.addActionListener(app.actionOpen);
		menu.add(item);
		
		if (onMacOSX == false) {
			menu.addSeparator();
			
			item = new JMenuItem("Quit");
			item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
			item.addActionListener(app.actionQuit);
			menu.add(item);
		}
		
		return menu;
	}
	
	private JMenu createWindowMenu() {
		JMenu menu = new JMenu("Window");
		JMenuItem item;
		
		item = new JMenuItem("Minimize");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		item.addActionListener(app.actionMin);
		menu.add(item);
		
		item = new JMenuItem("Bring All to Front");
		item.addActionListener(app.actionBringAllToFront);
		menu.add(item);
		return menu;
	}
	
	private void createMenu() {
		this.add(createFileMenu());
		this.add(createWindowMenu());
		this.add(new JMenu("Help"));
	}
}
